package com.example.sofa_module;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.common.EvenBusAction;
import com.example.common.mvvm.view.BaseFragment;
import com.example.sofa_module.databinding.FragmentTabBinding;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class TabFragment extends BaseFragment<FragmentTabBinding,TabViewModel>  implements OnItemChildClickListener{
    private List<SofaFeedEntity.DataBean.VideoData>txtList=new ArrayList<>();
    private List<SofaFeedEntity.DataBean.VideoData>picList=new ArrayList<>();
    private List<SofaFeedEntity.DataBean.VideoData>videoList=new ArrayList<>();
    private SofaAdapter sofaAdapter;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        FragmentTabBinding binding=FragmentTabBinding.inflate(inflater);
        return binding;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        vm.list.observe(this, new Observer<List<SofaFeedEntity.DataBean.VideoData>>() {
            @Override
            public void onChanged(List<SofaFeedEntity.DataBean.VideoData> videoData) {
                for (SofaFeedEntity.DataBean.VideoData videoDatum : videoData) {
                    if (videoDatum.itemType==0){
                        videoDatum.type=0;
                        txtList.clear();
                        txtList.add(videoDatum);
                    }else if (videoDatum.itemType==1){
                        videoDatum.type=1;
                        picList.clear();
                        picList.add(videoDatum);
                    }else if (videoDatum.itemType==2){
                        videoDatum.type=2;
                        videoList.clear();
                        videoList.add(videoDatum);
                    }
                }
                sofaAdapter=new SofaAdapter();
                sofaAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                        List<SofaFeedEntity.DataBean.VideoData> data = (List<SofaFeedEntity.DataBean.VideoData>) adapter.getData();
                        SofaFeedEntity.DataBean.VideoData videoData1 = data.get(position);
                        ARouter.getInstance().build("/sofa/DetailActivity").withSerializable("videobean",videoData1).navigation();
                    }
                });
                LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                layoutManager.setOrientation(RecyclerView.VERTICAL);
                v.sofaRecycler.setLayoutManager(layoutManager);
                v.sofaRecycler.setAdapter(sofaAdapter);
                if (picList!=null){
                    sofaAdapter.setNewInstance(picList);
                }
                share(sofaAdapter);
            }
        });
    }

    private void share(SofaAdapter sofaAdapter) {
        sofaAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void selectTab(EvenBusAction evenBusAction){
        if (evenBusAction.action.equals("selectTab")){
            int position = (int) evenBusAction.map.get("position");
            switch (position){
                case 0:{
                    if (picList!=null){
                        sofaAdapter.setNewInstance(picList);
                    }
                }break;
                case 1:{
                    if (videoList!=null){
                        sofaAdapter.setNewInstance(videoList);
                    }
                }break;
                case 2:{
                    if (txtList!=null){
                        sofaAdapter.setNewInstance(txtList);
                    }
                }break;
            }
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        if (view.getId()==R.id.item_share){
            new ShareAction(getActivity()).withText("hello").setPlatform(SHARE_MEDIA.SINA)
                    .share();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(getActivity()).onActivityResult(requestCode, resultCode, data);
    }
}
